package components;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;

import javax.swing.SwingConstants;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;

import client.Client;

import components.listeners.GoButtonActionListener;

public class AuthenticationContentPanel extends JPanel
{

	private static final long serialVersionUID = 1L;
	private JLabel usernameLabel;
	private JLabel passwordLabel;
	private JButton goButton;
	private JTextField usernameTextField;
	private JPasswordField passwordTextField;
	private JLabel statusLabel;
	private JComboBox actionComboBox;
	
	public AuthenticationContentPanel(Client client)
	{
		this.buildComponents(client);
	}
	private void buildComponents(Client client)
	{
		goButton = new JButton("Go");
		passwordTextField = new JPasswordField(20);
		passwordLabel = new JLabel("Password");
		usernameLabel = new JLabel("Username");
		statusLabel = new JLabel();
		usernameTextField = new JTextField(20);
		actionComboBox = new JComboBox(new String[] { "Login", "Create Account", "Change Password", "Restart Server", "Shutdown Server" });
		
		goButton.setBounds(170, 159, 159, 23);
		
		passwordLabel.setBounds(117, 56, 71, 14);
		passwordTextField.setBounds(192, 53, 166, 20);
		
		usernameLabel.setBounds(115, 25, 73, 14);
		usernameTextField.setBounds(192, 22, 166, 20);
		
		statusLabel.setHorizontalAlignment(SwingConstants.CENTER);
		statusLabel.setBounds(12, 85, 476, 16);
	
		actionComboBox.setBounds(170, 122, 159, 25);
		
		setLayout(null);
		add(statusLabel);
		add(goButton);
		add(passwordTextField);
		add(passwordLabel);
		add(usernameLabel);
		add(usernameTextField);
		add(actionComboBox);
		
		addListeners(client);
	}
	public String getUsername()
	{
		return usernameTextField.getText();
	}
	public String getPassword()
	{
		return new String(passwordTextField.getPassword());
	}
	public String getAction()
	{
		return (String) this.actionComboBox.getSelectedItem();
	}
	public void setStatusText(String text)
	{
		this.statusLabel.setText(text);
	}
	private void addListeners(Client client)
	{
		GoButtonActionListener loginListener = null;
		
		try
		{
			loginListener = new GoButtonActionListener(this, client);
		}
		catch (InstantiationException e)
		{
			e.printStackTrace();
		}
		
		if (loginListener != null)
		{
			goButton.addActionListener(loginListener);
			usernameTextField.addActionListener(loginListener);
			passwordTextField.addActionListener(loginListener);
		}
	}
}
